package com.southminority.ethnic.controller.user;

import com.southminority.ethnic.common.context.UserContext;
import com.southminority.ethnic.common.result.R;
import com.southminority.ethnic.pojo.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户信息Controller
 * 演示如何使用UserContext获取当前登录用户信息
 * 
 * @author SouthMinority
 */
@Slf4j
@Tag(name = "用户信息", description = "用户信息相关接口")
@RestController
@RequestMapping("/user/profile")
public class UserProfileController {

    /**
     * 获取当前登录用户信息
     */
    @Operation(summary = "获取当前登录用户信息")
    @GetMapping("/current")
    public R<User> getCurrentUser() {
        try {
            User currentUser = UserContext.getCurrentUser();
            if (currentUser == null) {
                return R.fail("用户未登录");
            }
            
            // 清除敏感信息
            currentUser.setPassword(null);
            currentUser.setToken(null);
            
            log.info("获取当前用户信息成功，用户ID: {}", currentUser.getId());
            return R.ok(currentUser);
            
        } catch (Exception e) {
            log.error("获取当前用户信息失败", e);
            return R.fail("获取用户信息失败：" + e.getMessage());
        }
    }

    /**
     * 获取当前用户ID
     */
    @Operation(summary = "获取当前用户ID")
    @GetMapping("/userId")
    public R<Long> getCurrentUserId() {
        try {
            Long userId = UserContext.getCurrentUserId();
            if (userId == null) {
                return R.fail("用户未登录");
            }
            
            log.info("获取当前用户ID成功: {}", userId);
            return R.ok(userId);
            
        } catch (Exception e) {
            log.error("获取当前用户ID失败", e);
            return R.fail("获取用户ID失败：" + e.getMessage());
        }
    }

    /**
     * 获取当前用户邮箱
     */
    @Operation(summary = "获取当前用户邮箱")
    @GetMapping("/email")
    public R<String> getCurrentUserEmail() {
        try {
            String email = UserContext.getCurrentUserEmail();
            if (email == null) {
                return R.fail("用户未登录");
            }
            
            log.info("获取当前用户邮箱成功: {}", email);
            return R.ok(email);
            
        } catch (Exception e) {
            log.error("获取当前用户邮箱失败", e);
            return R.fail("获取用户邮箱失败：" + e.getMessage());
        }
    }

    /**
     * 检查用户是否已登录
     */
    @Operation(summary = "检查用户是否已登录")
    @GetMapping("/isLoggedIn")
    public R<Boolean> isLoggedIn() {
        try {
            boolean isLoggedIn = UserContext.isLoggedIn();
            log.info("检查用户登录状态: {}", isLoggedIn);
            return R.ok(isLoggedIn);
            
        } catch (Exception e) {
            log.error("检查用户登录状态失败", e);
            return R.fail("检查登录状态失败：" + e.getMessage());
        }
    }
} 